package arm_search_2;

import data.Seed;
import data.email.EmailSeed;
import java.util.ArrayList;
import process.email.EmailIndexGenerator;
import query_processing.email.EmailQueryProcessor;

/**
 * Kelas program utama yang menerima dua mode eksekusi, yaitu build index dan
 * query term Mode build index dipanggil dengan parameter : build
 * <email_directory> <thread_count> email_directory : root dari directory berisi
 * email yang akan diindex thread_count : jumlah thread yang akan digunakan
 * untuk mengeksekusi proses indexing
 *
 * Mode eksekusi query dipanggil dengan parameter : query <index_directory>
 * <field_name:token_string> index_directory : direktori tempat file indeks
 * hasil indexing field_name : field yang akan diquery statistiknya, bernilai
 * from, to, body, subject, atau date
 *
 * @author Muqtafi Akhmad
 */
public class ARM_Search_2 {

    public static void main(String[] args) {
        if (args.length != 4) {
            System.out.println("Invalid argument count");
            System.out.print("## Arguments for retrieval should be : query <index_directory> <field_name:token_string> <is_compressed : 1 / 0>");
            System.out.print("## Arguments for build index should be : build <email_directory> <worker_number> <is_conmpressed : 1 / 0>");
            System.exit(1);
        } else {
            boolean isCompressed = false;
            if (args[3].equals("1")) {
                isCompressed = true;
            }
            if (args[0].equals("build")) {
                // build index
                String databaseDirectory = args[1];
                Integer threadCount = Integer.parseInt(args[2]);
                Seed rootSeed = new EmailSeed(databaseDirectory);
                ArrayList<String> seedFilter = new ArrayList<String>();
                seedFilter.add("all_documents");
                EmailIndexGenerator indexGenerator = new EmailIndexGenerator(threadCount);
                System.out.println("## Reading statistics for directory " + databaseDirectory);
                long startTime = System.currentTimeMillis(), endTime;
                indexGenerator.doGenerateIndex(rootSeed, seedFilter, isCompressed);
                endTime = System.currentTimeMillis();
                System.out.println("## Statistics reading finished , elapsed time " + ((endTime - startTime) / 1000) + " seconds");
                System.exit(0);
            } else if (args[0].equals("query")) {
                // do query
                String indexDirectory = args[1];
                String tokenDetail[] = args[2].split(":");
                String fieldName = tokenDetail[0];
                String tokenString = tokenDetail[1];

                EmailQueryProcessor instance = new EmailQueryProcessor();
                System.out.println("## Executing query for '" + tokenString + "' in field '" + fieldName + "'");
                instance.doProcessQuery(indexDirectory, fieldName, tokenString, isCompressed);
            } else {
                System.out.println("## Unknown first parameter : " + args[0]);
            }
        }
    }
}
